#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:Vergil
# datetime:2018/10/21 22:41
# software: PyCharm
import logging
import time

import requests
import re

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

logger = logging.getLogger('main.f_collect')


def course_choose(driver, course_name):
    try:
        courseUrl = driver.find_element_by_xpath("//a[contains(@title,'%s')]" % course_name).get_attribute(
            'data-2')
        driver.get('https://spoc.tfswufe.edu.cn' + courseUrl)

        nextUrl = driver.find_element_by_xpath(
            "//a[text()='资料下载']").get_attribute('href')

        driver.get(nextUrl)

        logger.info('已成功进入资料下载页面，请继续')

    except Exception:
        logger.info('------未找到该课程，请检查输入是否正确------')
        name = input('重新输入课程名')
        course_choose(driver, name)


def get_file_urls(driver):
    file_url_lst = []
    i = 0
    n = 1
    while i < n:
        try:
            wait = WebDriverWait(driver, 10)
            element = wait.until(
                EC.presence_of_element_located(
                    (By.XPATH, '//div[@id="fileContent"]/table')))
            file_elements = element.find_element_by_tag_name(
                'tbody').find_elements_by_tag_name('tr')
            li_page = driver.find_elements_by_xpath(
                "//div[@id='page2']/ul/li")[-1].get_attribute('data-lp')

            for file_element in file_elements:
                try:
                    file_url = file_element.find_element_by_tag_name(
                        'a').get_attribute('href')
                    file_url_lst.append(file_url)
                except Exception as e:
                    logger.exception(e)

            page = page_turn(driver)
            next_page = int(page) + 1
            try:
                while page == li_page:
                    logger.info('-------已是最后一页-------')
                    logger.info('-------文件链接抓取完成-------')
                    return file_url_lst

                if int(page) % 10 == 0:
                    driver.find_element_by_class_name(
                        'next').find_element_by_tag_name('a').click()
                    logger.info('------即将点击第%s页------' % next_page)
                else:
                    wait.until(
                        EC.presence_of_element_located(
                            (By.ID, 'page2')))
                    driver.find_element_by_xpath(
                        "//ul/li/a[text()=%s]" %
                        str(next_page)).click()
                    logger.info('------即将点击第%s页------' % next_page)

            except Exception as e:
                logger.info('翻页失败')
            time.sleep(2)
            n = next_page
            i += 1

        except TypeError as e:
            logger.exception(e)
        except Exception as e:
            logger.exception(e)
    return file_url_lst


def file_download(file_url_lst, headers):
    logger.info('开始下载文件')
    i = 1
    for file_url in file_url_lst:
        try:
            requests.get(file_url, headers=headers, stream=True)
            logger.info('>>>>>>下载第%d个文件<<<<<<' % i)
            time.sleep(1)

        except Exception as e:
            logger.info(e)
            continue
        i += 1
    logger.info('------下载文件完成------')


def page_turn(driver):
    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, 'page2'))
        )

        current_page = element.find_element_by_xpath(
            '//ul/li[@class="disabled"]')
        if current_page.text != '':
            return current_page.text

    except Exception as e:
        logger.exception(e)


def main(driver, headers, course_name):
    course_choose(driver, course_name)
    file_url_lst = get_file_urls(driver)
    file_download(file_url_lst, headers)
